package com.code.pattern.strategy;

import com.code.pattern.strategy.activity.PromotionActivity;
import com.code.pattern.strategy.factory.PromotionStrategyFactory;
import com.code.pattern.strategy.promotion.CashbackStrategy;
import com.code.pattern.strategy.promotion.CouponStrategy;
import org.apache.commons.lang3.StringUtils;

/**
 * @author: liyufei
 * @date: 2021-04-01 20:08
 */
public class PromotionActivityTest {

    public static void main(String[] args) {

        // 测试策略模式，选用不同的策略
        // test();

        // 优化代码，使其更符合业务场景
        // testScene();

        // 优化代码，单例模式+工厂模式
        testSceneRefactor();
    }

    private static void test() {
        PromotionActivity activity618 = new PromotionActivity(new CouponStrategy());
        activity618.execute();

        PromotionActivity activity11 = new PromotionActivity(new CashbackStrategy());
        activity11.execute();
    }

    /**
     * 如下代码的弊端在于，活动策略可能越来越多，我们接下来应该优化此代码
     */
    private static void testScene() {
        PromotionActivity activity = null;
        String promotionKey = "COUPON";

        if (StringUtils.equals(promotionKey, "COUPON")) {
            activity = new PromotionActivity(new CouponStrategy());
        } else if (StringUtils.equals(promotionKey, "CASHBACK")) {
            activity = new PromotionActivity(new CashbackStrategy());
        } // ...

        activity.execute();

    }

    /**
     * 优化代码，单例模式+工厂模式
     */
    private static void testSceneRefactor() {
        String promotionKey = "COUPON";

        PromotionActivity activity = new PromotionActivity(PromotionStrategyFactory.getPromotionStrategy(promotionKey));
        activity.execute();
    }

}
